package com.lagou.rabbitmqttl.controller;

import com.lagou.rabbitmqttl.listener.RabbitmqListener;
import com.lagou.rabbitmqttl.pojo.Order;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.UUID;

@RestController
public class PayController {

    @Autowired
    private AmqpTemplate rabbitTemplate;

    @RequestMapping("/order")
    public String order() {
        Order order = new Order();
        order.setOrderNum(UUID.randomUUID().toString());
        RabbitmqListener.orders.put(order.getOrderNum(),order);
        rabbitTemplate.convertAndSend("ex.pay", "pay", order);
        return "订单号" + order.getOrderNum() + "已下单，请在三十秒内支付。。。";
    }

    @RequestMapping("/pay/{orderNum}")
    public String pay(@PathVariable String orderNum) {
        Order order = RabbitmqListener.orders.get(orderNum);
        String message = "支付状态未知";
        if(order.getPayStatus() == Order.UN_PAY){
            order.setPayStatus(Order.PAY_SUCCESS);
            RabbitmqListener.orders.put(order.getOrderNum(),order);
            message = "支付成功";
        }else if(order.getPayStatus() == Order.PAY_TIMEOUT){
            message = "支付超时";
        }else if(order.getPayStatus() == Order.PAY_SUCCESS){
            message = "订单已支付，请勿重复提交";
        }
        return message;
    }


}
